package com.ow.framework.listener.event.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.ow.framework.common.Constant;
import com.ow.framework.data.po.ResultData;
import com.ow.framework.listener.event.EventExecutor;
import com.ow.framework.listener.event.EventListener;
import com.ow.framework.util.Encrypt;
import com.opensymphony.xwork2.ActionContext;




public class QueryPmsEvent implements EventListener{

	
	/**异常处理*/
	static Logger log = Logger.getLogger(Constant.FRAMEWORK);
	private ResultData resultData = new ResultData();
	
	public ResultData event(EventExecutor eventExecutor) throws Exception {

		try
		{ 
			String ssRank = eventExecutor.getBaseAction().getSession(true).getAttribute("randname")==null?"":eventExecutor.getBaseAction().getSession(true).getAttribute("randname").toString();
			String usRank = eventExecutor.getBaseAction().getParamMap().get("rankNum")==null?"":eventExecutor.getBaseAction().getParamMap().get("rankNum").toString();
			if("".equals(usRank)||!ssRank.equals(usRank)){
				eventExecutor.getBaseAction().getPromptInfo().setResultDescInfo(eventExecutor.getBaseAction().getText("rank_error"));
				resultData.setIntResult(0);  
				throw new Exception ("login rank error");
			}
			
//			 session.get(Constant.S_USER_LOGIN_ACCOUNT);
			String account = eventExecutor.getBaseAction().getParamMap().get("account");
			String password = eventExecutor.getBaseAction().getParamMap().get("password");
			/**转换密码为md5格式*/
			if (!"".equals(password))
				password = Encrypt.getMD5(password.trim());
			String sid = eventExecutor.getSqlidByIndex(0);
			String pmsAllSqlID = eventExecutor.getSqlidByIndex(1);
			String pmsSqlID = eventExecutor.getSqlidByIndex(2);
			String nodeAllSqlID = eventExecutor.getSqlidByIndex(3);
			String nodeSqlID = eventExecutor.getSqlidByIndex(4);
			/**验证登录信息*/
			if (!"".equals(sid))
			{
				Object one = eventExecutor.getSqlSession().selectOne(sid, eventExecutor.getBaseAction().getParamMap());
				if(one != null){
					
					if (one instanceof HashMap)
					{
						String pwd = ((HashMap)one).get("userPassword") == null?"":((HashMap)one).get("userPassword").toString();
						if (!password.equals(pwd.toLowerCase()))
						{
							/**取密码错误信息*/
							eventExecutor.getBaseAction().getPromptInfo().setResultDescInfo(eventExecutor.getBaseAction().getText("password_error"));
							resultData.setIntResult(0);  
							throw new Exception ("login password error");
						}
						String userType = ((HashMap)one).get("userType") == null?"":((HashMap)one).get("userType").toString();
						List listPms = null,listNode = null;
						if (userType.equals("0")){
							if (!pmsAllSqlID.equals("")){
								listPms = eventExecutor.getSqlSession().selectList(pmsAllSqlID, ((HashMap)one));
							}
							if (!nodeAllSqlID.equals("")){
								listNode = eventExecutor.getSqlSession().selectList(nodeAllSqlID, ((HashMap)one));
							}							
						}
						else{
							if (!pmsSqlID.equals("")){
								listPms = eventExecutor.getSqlSession().selectList(pmsSqlID, ((HashMap)one));
							}		
							if (!nodeSqlID.equals("")){
								listNode = eventExecutor.getSqlSession().selectList(nodeSqlID, ((HashMap)one));
							}								
						}
						/**存放登录用户权限信息到session*/
						eventExecutor.getBaseAction().session.put(Constant.S_LOGINC_PERMISSION,listPms);
						/**设置登录用户栏目权限*/
						eventExecutor.getBaseAction().session.put(Constant.S_NODE, listNode);						
						/**设置登陆用户id*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_ID, ((HashMap)one).get("userID"));
						/**设置登录者机构代码*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_ORG_ID, ((HashMap)one).get("userOrgID"));
						/**设置登录者登录IP*/
						HttpServletRequest request = ServletActionContext.getRequest();               
						eventExecutor.getBaseAction().session.put(Constant.S_USER_LOGIN_IP, request.getRemoteAddr());
						/**设置登录者账号*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_LOGIN_ACCOUNT, account);
						/**设置登录者类型*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_TYPE, ((HashMap)one).get("userType"));
						/**设置登录者姓名*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_LOGIN_NAME, ((HashMap)one).get("userName"));
						/**设置登录者邮件*/
						eventExecutor.getBaseAction().session.put(Constant.S_USER_EMAIL, ((HashMap)one).get("userEmail"));
						eventExecutor.getBaseAction().session.put("QUERY_CODE", Constant.QUERY_CODE);
						eventExecutor.getBaseAction().session.put("HANDLE_CODE", Constant.HANDLE_CODE);
					}
					setSessionInfo2ParamMap(eventExecutor.getBaseAction().getParamMap());
					resultData.setIntResult(1);
				}else{
					eventExecutor.getBaseAction().getPromptInfo().setResultDescInfo(eventExecutor.getBaseAction().getText("account_error"));
					resultData.setIntResult(0); 
					throw new Exception ("login account error");
				}
			}	
			return resultData;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	//登陆时就把session放到paramMap中，否则用户登陆的操作日志记录不完整
	private void setSessionInfo2ParamMap(Map paramMap){
		/***********************************************************/
		/**获取session中的对象到paramMap参数对象中*/
		/***********************************************************/
		Map session = ActionContext.getContext().getSession();
		Iterator iterator = session.keySet().iterator();
		while (iterator.hasNext())
		{
			Object obj = iterator.next();
			Object objValue = session.get(obj.toString());
			if (objValue instanceof String || objValue instanceof Integer || objValue instanceof BigDecimal || objValue instanceof Short)
			{
				paramMap.put(obj.toString(), objValue.toString());
			}
		}
	}

}
